<?php

namespace App\Http\Controllers\Manage;

use App\Model\Feedback;
use App\Model\FeedbackCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;


class FeedbackController extends Controller
{
    /**
     * //定义一个数组  用来排列查询选项或列表头
     * @var array
     */
    protected static $field = [
        ['id'=>1,'navigation'=>'编号'],
        ['id'=>2,'navigation'=>'提交时间'],
        ['id'=>3,'navigation'=>'所属类别'],
        ['id'=>4,'navigation'=>'钉钉ID'],
        ['id'=>5,'navigation'=>'用户姓名'],
        ['id'=>6,'navigation'=>'手机号'],
        ['id'=>7,'navigation'=>'qq号']
    ];

    /**
     * 反馈列表(全部)
     */
    public function index()
    {
        $type = Input::get('type');
        $param = Input::get('param');
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
        $id = Input::get('id');
        $obj = new Feedback();
        if($type == 1){
            $obj = $obj->where('id','like',"%$param%");
        }elseif ($type == 2){
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif ($type == 3){
            $cate = array();
            if(strpos('问题反馈',$param) !== false)$cate[] = 1;
            if(strpos('建议意见',$param) !== false)$cate[] = 2;
            if(strpos('其他',$param) !== false)$cate[] = 3;
            $obj = $obj->whereIn('category',$cate);
        }elseif ($type == 4){
            $obj = $obj->where('dd_id','like',"%$param%");
        }
        #根据类型id获取对应反馈信息
        if($id){
            $obj = $obj->where('category',$id);
        }
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $feedback = $obj->paginate(10);
        if(empty($page)){
            $count = $obj->count();
        }
        #根据类别id获取类别信息
        foreach ($feedback as $k=>$f){
            $feedback[$k]->categoryName = FeedbackCategory::getFeedbackCategoryName($f->category);
        }
        #获取所有反馈类别
        $feedbackCategory = FeedbackCategory::getAllCateGory();
        #获取表格头
        $navigation = self::$field;
        return view('manage.feedback.feedback_list',[
            'feedback'=>$feedback,
            'feedbackCategory'=>$feedbackCategory,
            'navigation'=>$navigation,
            'type'=>$type,
            'typeid'=>$id,
            'count'=>$count,
            'param'=>$param,
            'orderBy'=>$orderBy,
            'active1'=>10
        ]);
    }

    /**
     * 删除反馈信息
     * @return array
     */
    public function deleteFeedback()
    {
        $id = Input::get('id');
        $res = Feedback::deleteFeedback($id);
        if ($res) {
            return [
                'error' => 0,
                'msg' => '删除成功！'
            ];
        } else {
            return [
                'error' => 1,
                'msg' => '删除失败！'
            ];
        }
    }
}
